package n1;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author wxc
 * @date 2021年09月14日 17:13
 */
public class Client {
    public static void main(String[] args) {
        Socket socket = null;
        BufferedOutputStream bufferedOutputStream = null;
        String fileName = "./img.png";
        try {
            socket = new Socket(InetAddress.getLocalHost(), 9998);
            // 读取文件到byte[]数组并发送
            byte[] bytes = StreamUtils.streamToByteArray(new BufferedInputStream(new FileInputStream(fileName)));
            bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
            bufferedOutputStream.write(bytes);
            socket.shutdownOutput();
            // 接受来自服务端的响应
            String s = StreamUtils.streamToString(socket.getInputStream());
            System.out.println(s);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedOutputStream != null) {
                    bufferedOutputStream.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
